package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
	static Connection conn = null;
	public static Connection getconn() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/aa?characterEncoding=utf-8","root","root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeAll(Connection conn,PreparedStatement ps, ResultSet rs) throws SQLException {
		if(rs!=null) {
			rs.close();
		}
		if(ps!=null) {
			ps.close();
		}
		if(conn!=null) {
			conn.close();
		}
	}
	public static int exeUpd(String sql,String[] params) throws Exception {
		int flag=0;
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtil.getconn();
		ps = conn.prepareStatement(sql);
		for(int i=0;i<params.length;i++) {
			ps.setString(i+1, params[i]);
		}
		flag = ps.executeUpdate();
		System.out.println(flag);
		return flag;
	}
	public static int exeUpd1(String sql,String[] params) {
		int flag=0;
		int i=0;
		Connection conn = null;
		PreparedStatement ps = null;
		conn = DBUtil.getconn();
		try {
			ps = conn.prepareStatement(sql);
			for(;i<params.length-1;i++) {
				ps.setString(i+1, params[i]);
			}
			ps.setInt(params.length, Integer.parseInt(params[i]));
			ps.setInt(params.length, Integer.parseInt(params[i]));
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		System.out.println(flag);
		return flag;
	}
	
}
